package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbMenuMapper;
import com.bwie.pojo.TbMenu;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.Result;
import com.bwie.vo.PageUserVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
* @author 31803
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-05-21 13:43:48
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{
    @Autowired
    TbMenuMapper tbMenuMapper;
    @Autowired
    TbUserMapper tbUserMapper;
    @Autowired
    FastFileStorageClient fileStorageClient;

    @Override
    public List geteMenuTree(Integer userId) {
      List<TbMenu> tbMenuList=  tbMenuMapper.geteMenuTree(userId);
        List<Tree<String>> menuPath = TreeUtil.build(tbMenuList, "0", (menu, tree) -> {
            tree.setId(menu.getMenuId() + "");
            tree.setName(menu.getMenuName());
            tree.setParentId(menu.getMenuPid() + "");
            tree.putExtra("menuPath", menu.getMenuPath());
        });
        return menuPath;
    }



    @Override
    public Result UserList(PageUserVo pageUserVo) {
        Page<TbUser> page = new Page<>(pageUserVo.getPageNum(),pageUserVo.getPageSize());
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        if(StringUtil.isNotEmpty(pageUserVo.getKeyWord())){
            wrapper.lambda().or().like(TbUser::getUserName,pageUserVo.getKeyWord());
            wrapper.lambda().or().like(TbUser::getNickName,pageUserVo.getKeyWord());
            wrapper.lambda().or().like(TbUser::getUserMobile,pageUserVo.getKeyWord());
            wrapper.lambda().or().like(TbUser::getUserEmail,pageUserVo.getKeyWord());
        }
        if(pageUserVo.getGender()!=null){
            wrapper.lambda().eq(TbUser::getGender,pageUserVo.getGender());
        }
        if(StringUtil.isNotEmpty(pageUserVo.getStartTime())){
            wrapper.lambda().ge(TbUser::getCreateTime,pageUserVo.getStartTime());
        }
        if(StringUtil.isNotEmpty(pageUserVo.getEndTime())){
            wrapper.lambda().le(TbUser::getCreateTime,pageUserVo.getEndTime());
        }
       Page<TbUser> userLIst= tbUserMapper.selectPage(page,wrapper);
        return Result.ok().setData(userLIst);
    }

    @Override
    public Result UpdataUser(TbUser tbUser) {
        tbUserMapper.updateById(tbUser);
        return Result.ok();
    }

    @Override
    public Result UpimageFile(MultipartFile file) {
        String url= null;
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return Result.ok().setData(url).setMsg("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().setCode(500).setMsg("上传失败");
        }

    }

    @Override
    public Result deleteUserById(Integer userId) {
        //判断用户是否存在
        TbUser user = tbUserMapper.selectById(userId);
        if(user==null){
            return Result.error().setCode(500).setMsg("用户不存在");
        }

        //删除用户
        tbUserMapper.deleteById(userId);
        return Result.ok();
    }

    @Override
    public Result deleteUserAll(List<TbUser> userList) {
        //校验参数
        if(userList==null||userList.size()==0){
            return Result.error().setCode(500).setMsg("请选择数据");
        }
        for (TbUser user : userList) {
            tbUserMapper.deleteById(user.getUserId());
        }
        return Result.ok();
    }

    @Override
    public Result CheckUserUpdata(TbUser tbUser) {
        TbUser user = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, tbUser.getUserName())
        );

        if(user==null){
            return Result.error().setCode(500).setMsg("用户不存在");
        }
        if(tbUser.getUserStatus()==2){
                if(StringUtil.isEmpty(tbUser.getReason())){
                    return Result.error().setCode(500).setMsg("驳回条件不能为空");
                }
        }

        user.setUserStatus(tbUser.getUserStatus());
        user.setReason(tbUser.getReason());
        tbUserMapper.updateById(user);
        return Result.ok();
    }
}




